﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Company.Merchandising.BusinessLayer.DataEntities;
using Company.Merchandising.BusinessLayer.DataTransferObjects;
using Company.Merchandising.DataAccessLayer.DataAccessCommon;
using Company.Merchandising.ServiceLayer.EntityManagement.Constants;

namespace Company.Merchandising.ServiceLayer.EntityManagement
{
	public class CityManager : BaseManager
	{
		public CityManager(MerchModelEntities modelEntities) : base(modelEntities) { }

		public void AddCity(DTOCity dtoCity)
		{
			if (dtoCity.Province == null)
			{
				throw new InvalidOperationException(string.Format(ExceptionMessages.SelectProvinceForCity, dtoCity.Name));
			}

			DACCity dacCity = new DACCity(_modelEntities);
			DACProvince dacProvince = new DACProvince(_modelEntities);
			Province cityProvince = dacProvince.GetProvinceById(dtoCity.Province.Id);
			if (cityProvince == null)
			{
				throw new InvalidOperationException(string.Format(ExceptionMessages.ProvinceDoesNotExistAnyMore, dtoCity.Province.Name));
			}

			if (dacCity.GetCitiesByNameInProvince(dtoCity.Name, dtoCity.Province.Id).Count() > 0)
			{
				throw new InvalidOperationException(string.Format(ExceptionMessages.CityAlreadyExistInProvince, dtoCity.Name, dtoCity.Province.Name));
			}

			var newCity = new City();
			newCity.Name = dtoCity.Name;
			newCity.Province = cityProvince;
			_modelEntities.AddObject(newCity.GetType().Name, newCity);
			_modelEntities.SaveChanges();
		}

		public void DeleteCity(int cityId)
		{
			DACCity dacProvince = new DACCity(_modelEntities);
			var foundCity = dacProvince.GetCityById(cityId);
			if (foundCity == null)
			{
				throw new InvalidOperationException(string.Format(ExceptionMessages.CityDoesNotExistAnyMore, cityId));
			}

			if (!foundCity.Addresses.IsLoaded)
			{
				foundCity.Addresses.Load();
			}

			if (foundCity.Addresses.Count > 0)
			{
				throw new InvalidOperationException(string.Format(ExceptionMessages.CityPossessesCities, cityId));
			}

			_modelEntities.DeleteObject(foundCity);
			_modelEntities.SaveChanges();
		}

		public void UpdateCity(DTOCity dtoCity)
		{
			DACCity dacCity = new DACCity(_modelEntities);
			DACProvince dacProvince = new DACProvince(_modelEntities);
			if (dacCity.GetCitiesByNameAndOtherIdInProvince(dtoCity.Name, dtoCity.Id, dtoCity.Province.Id).Count() > 0)
			{
				throw new InvalidOperationException(string.Format(ExceptionMessages.CityAlreadyExistInProvince, dtoCity.Name, dtoCity.Province.Name));
			}

			var province = dacProvince.GetProvinceById(dtoCity.Province.Id);
			if (province == null)
			{
				throw new InvalidOperationException(string.Format(ExceptionMessages.ProvinceDoesNotExistAnyMore, dtoCity.Province.Name));
			}

			var foundCity = dacCity.GetCityById(dtoCity.Id);
			if (foundCity == null)
			{
				throw new InvalidOperationException(string.Format(ExceptionMessages.CityDoesNotExistAnyMore, dtoCity.Id));
			}

			foundCity.Name = dtoCity.Name;
			foundCity.Province = province;
			_modelEntities.SaveChanges();
		}
	}
}
